package com.hzsk.lisence.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.AnnotationForLog;
import com.hzsk.lisence.entity.LogEnum;
import com.hzsk.lisence.entity.SoftwareSonEntity;
import com.hzsk.lisence.entity.query.SelectByNameQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.SoftwareSonMapper;
import com.hzsk.lisence.service.SoftwareSonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class SoftwareSonServiceImpl implements SoftwareSonService {


    @Resource
    private SoftwareSonMapper softwareSonMapper;
    @Override
    public List<SoftwareSonEntity> getSoftwareListSonInfo(Integer softwareId) {
        return softwareSonMapper.getSoftwareListSonInfo(null,softwareId,1);
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.CREATE_SON_SOFTWARE,operation = "创建软件子模块",operationalEntity = "SoftwareSonEntity.java/STUDENT_T")
    public void saveSoftwareSonInfo(SoftwareSonEntity softwareSonEntity,Integer userId, IncResponce incResponce) {
        List<SoftwareSonEntity> softwareSonInfoByName = softwareSonMapper.getSoftwareSonInfoByName(1, softwareSonEntity.getSoftwareSonName());
        if (softwareSonInfoByName .size() > 0 ) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("子模块名称已存在");
            return;
        }
        softwareSonEntity.setSaveType(1);
        softwareSonMapper.saveSoftwareSonInfo(softwareSonEntity);
        incResponce.setCode(0);
        incResponce.setData("操作成功");
    }

    @Override
    public Object getSoftwareSonListSelected(SelectByNameQuery selectByNameQuery) {
        PageHelper.startPage(selectByNameQuery.getPageNum(), selectByNameQuery.getPageSize());
        List<SoftwareSonEntity> infos = softwareSonMapper.getSoftwareSonInfoSelected(selectByNameQuery);
        return new PageInfo<>(infos);
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.UPDATE_SON_SOFTWARE,operation = "修改软件子模块",operationalEntity = "SoftwareSonEntity.java/STUDENT_T")
    public void updateSoftwareSonInfo(SoftwareSonEntity softwareSonEntity,Integer userId,IncResponce incResponce) {
        List<SoftwareSonEntity> softwareSonInfoByName = softwareSonMapper.getSoftwareSonInfoByName(1, softwareSonEntity.getSoftwareSonName());
        if (softwareSonInfoByName .size() >= 1 && !Objects.equals(softwareSonInfoByName.get(0).getId(), softwareSonEntity.getId())) {
            incResponce.setCode(1);
            incResponce.setErrorMessage("子模块名称已存在");
            return;
        }
        softwareSonEntity.setSaveType(1);
        softwareSonMapper.updateSoftwareSonInfo(softwareSonEntity);
        incResponce.setCode(0);
        incResponce.setData("操作成功");
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.DEL_SON_SOFTWARE,operation = "修改软件子模块",operationalEntity = "SoftwareSonEntity.java/STUDENT_T")
    public String delSoftwareSonInfo(int[] ids,Integer userId) {
        for (Integer id : ids) {
            softwareSonMapper.delSoftwareSonInfo(id);
        }
        return "操作成功";
    }


}
